package com.zjlanyun.erp;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.zjlanyun.erp.network.WebApi;
import com.zjlanyun.erp.utils.Toastor;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;

import cz.msebera.android.httpclient.Header;

public class MainAddressBookDetail extends Activity {

	private LinearLayout ll_back;
	private TextView FMobilePhone, FPhone;
	private LinearLayout call_mobile, call_phone;
	private HashMap<String, Object> userinfo;
	private Toastor toastor;
	private TextView tv_surname, FName;
	private LinearLayout departmentWrap, phoneWrap, otherWrap, FHireDateWrap, FIDWrap, FGenderWrap, FDegreeWrap,
			FEmailWrap, FBirthdayWrap, FAddressWrap;
	private TextView FHireDate, FID, FGender, FDegree, FEmail, FBirthday, FAddress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_address_book_detail);

		initView();
	}

	// 初始化view
	private void initView() {
		ll_back = (LinearLayout) findViewById(R.id.ll_back);
		FMobilePhone = (TextView) findViewById(R.id.FMobilePhone);
		FPhone = (TextView) findViewById(R.id.FPhone);
		call_mobile = (LinearLayout) findViewById(R.id.call_mobile);
		call_phone = (LinearLayout) findViewById(R.id.call_phone);
		tv_surname = (TextView) findViewById(R.id.tv_surname);
		FName = (TextView) findViewById(R.id.FName);
		departmentWrap = (LinearLayout) findViewById(R.id.departmentWrap);
		phoneWrap = (LinearLayout) findViewById(R.id.phoneWrap);
		otherWrap = (LinearLayout) findViewById(R.id.otherWrap);
		FHireDateWrap = (LinearLayout) findViewById(R.id.FHireDateWrap);
		FIDWrap = (LinearLayout) findViewById(R.id.FIDWrap);
		FGenderWrap = (LinearLayout) findViewById(R.id.FGenderWrap);
		FDegreeWrap = (LinearLayout) findViewById(R.id.FDegreeWrap);
		FEmailWrap = (LinearLayout) findViewById(R.id.FEmailWrap);
		FBirthdayWrap = (LinearLayout) findViewById(R.id.FBirthdayWrap);
		FAddressWrap = (LinearLayout) findViewById(R.id.FAddressWrap);
		FHireDate = (TextView) findViewById(R.id.FHireDate);
		FID = (TextView) findViewById(R.id.FID);
		FGender = (TextView) findViewById(R.id.FGender);
		FDegree = (TextView) findViewById(R.id.FDegree);
		FEmail = (TextView) findViewById(R.id.FEmail);
		FBirthday = (TextView) findViewById(R.id.FBirthday);
		FAddress = (TextView) findViewById(R.id.FAddress);

		userinfo = Common.getUserinfo(this);
		toastor = new Toastor();

		if ((Boolean) userinfo.get("isDemo")) {
			departmentWrap.setVisibility(View.VISIBLE);
			phoneWrap.setVisibility(View.VISIBLE);
		} else {
			initData();
		}

		// 拨打手机号
		call_mobile.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String number = FMobilePhone.getText().toString();
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_DIAL);
				intent.setData(Uri.parse("tel:" + number));
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});

		// 拨打座机
		call_phone.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String number = FPhone.getText().toString();
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_DIAL);
				intent.setData(Uri.parse("tel:" + number));
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent);
			}
		});

		// 后退
		ll_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	// 配置数据
	private void initData() {
		Intent intent = getIntent();
		AsyncHttpClient client = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("username", userinfo.get("username"));
		params.put("password", userinfo.get("password"));
		params.put("type", "detail");
		params.put("FItemID", intent.getIntExtra("FItemID", 0));
		client.post(WebApi.addressbook(this), params, new TextHttpResponseHandler() {
			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				toastor.showShortToast(Data.SERVER_URL_ERROR);
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				JSONTokener jsonParser = new JSONTokener(responseString);
				try {
					JSONObject json = (JSONObject) jsonParser.nextValue();
					if (json.getInt("code") == 0) {
						JSONObject data = json.getJSONObject("data");
						if (!data.getString("FName").equals("")) {
							tv_surname.setText(data.getString("FName").substring(0, 1));
						}
						FName.setText(data.getString("FName"));

						// 电话
						if (!data.getString("FPhone").equals("")) {
							call_phone.setVisibility(View.VISIBLE);
							FPhone.setText(data.getString("FPhone"));
						}
						// 手机
						if (!data.getString("FMobilePhone").equals("")) {
							call_mobile.setVisibility(View.VISIBLE);
							FMobilePhone.setText(data.getString("FMobilePhone"));
						}
						if (!data.getString("FPhone").equals("") || !data.getString("FMobilePhone").equals("")) {
							phoneWrap.setVisibility(View.VISIBLE);
						}
						// 入职日期
						if (!data.getString("FHireDate").equals("")) {
							FHireDateWrap.setVisibility(View.VISIBLE);
							FHireDate.setText(data.getString("FHireDate"));
						}
						// 身份证
						if (!data.getString("FID").equals("")) {
							FIDWrap.setVisibility(View.VISIBLE);
							FID.setText(data.getString("FID"));
						}
						// 性别
						if (!data.getString("FGender").equals("")) {
							FGenderWrap.setVisibility(View.VISIBLE);
							FGender.setText(data.getString("FGender"));
						}
						// 学历
						if (!data.getString("FDegree").equals("")) {
							FDegreeWrap.setVisibility(View.VISIBLE);
							FDegree.setText(data.getString("FDegree"));
						}
						// Email
						if (!data.getString("FEmail").equals("")) {
							FEmailWrap.setVisibility(View.VISIBLE);
							FEmail.setText(data.getString("FEmail"));
						}
						// 生日
						if (!data.getString("FBirthday").equals("")) {
							FBirthdayWrap.setVisibility(View.VISIBLE);
							FBirthday.setText(data.getString("FBirthday"));
						}
						// 地址
						if (!data.getString("FAddress").equals("")) {
							FAddressWrap.setVisibility(View.VISIBLE);
							FAddress.setText(data.getString("FAddress"));
						}

					} else {
						toastor.showShortToast(json.getString("message"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
